﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DreamBuilder.Core.DAO;
using CommonModule;
using Microsoft.Practices.EnterpriseLibrary.Data.Oracle;
using ePlanet.SingleWell.Models;
using Web.SystemConfig.Models;

namespace ePlanet.SingleWell.DataAccess
{
    public class AttachmentDAL : DataHelper
    {
        public AttachmentDAL(string DBHelperString)
        {
            this.DBHelper = DBHelperFactory.CreateInstance(DBHelperString);
            if (!(this.DBHelper.database is OracleDatabase))
            {
                throw new Exception("只支持Oracle数据库");
            }
        }

        /// <summary>
        /// 保存业务数据和附件之间的关联，仅供附件上传控件使用。
        /// </summary>
        /// <param name="dt_Attach"></param>
        /// <param name="objectTableName"></param>
        /// <param name="objectID"></param>
        /// <param name="DBHelperString"></param>
        /// <returns></returns>
        internal bool SaveAttach(DataTable dt_Attach, string objectTableName, string objectID)
        {
            if (dt_Attach != null && dt_Attach.Rows.Count > 0)
            {
                foreach (DataRow arow in dt_Attach.Rows)
                {
                    if (int.Parse(arow["RowStatus"].ToString()) == 3) //状态为3则是删除该列
                    {
                        try
                        {
                            string sql = string.Format("Delete FROM P_BT_Attachments Where AttachmentID='{0}'", arow["AttachmentID"].ToString());
                            DBHelper.ExecuteSql(sql);
                        }
                        catch { return false; }
                    }
                    if (int.Parse(arow["RowStatus"].ToString()) == 2)//状态为2是修改该列
                    {
                        try
                        {
                            string sql = string.Format("Update P_BT_Attachments Set ObjectTableName='{0}',ObjectID='{1}' Where AttachmentID='{2}'", objectTableName, objectID, arow["AttachmentID"].ToString());
                            DBHelper.ExecuteSql(sql);
                        }
                        catch { return false; }
                    }
                }
            }
            return true;
        }

        /// <summary>
        /// 获取附件基本信息，不包含内容
        /// </summary>
        /// <param name="attachmentID"></param>
        /// <returns></returns>
        internal DataTable GetAttachmentByAttachmentID(string attachmentID)
        {
            string sql = string.Format("select attachmentid,attachmentname,uploadtime,attachmentsize,documenttype,objecttablename from p_bt_attachments where attachmentID='{0}'", attachmentID);
            DataSet oDataSet = DBHelper.ExecuteDataSet(sql);
            if (oDataSet != null && oDataSet.Tables.Count > 0)
            {
                return oDataSet.Tables[0];
            }
            else
            {
                return null;
            }
        }

        internal DataTable GetAttachmentListByObjectID(string attachmentID)
        {
            string sql = string.Format(@"select ATTACHMENTID,
       OBJECTTABLENAME,
       ATTACHMENTNAME,
       UPLOADTIME,
       ATTACHMENTSIZE,
       DOCUMENTTYPE,
       FILETYPEID,
       FILEURL,
       CREATEUSERID,
       REMARK
  from p_bt_attachments
  where OBJECTID='{0}'", attachmentID);
            DataSet oDataSet = DBHelper.ExecuteDataSet(sql);
            if (oDataSet != null && oDataSet.Tables.Count > 0)
            {
                return oDataSet.Tables[0];
            }
            else
            {
                return null;
            }
        }

        internal DataTable GetAttachmentListByObjectID(List<string> attachmentIDList)
        {
            string attachmentIDs = string.Empty;
            foreach (string attachmentID in attachmentIDList)
            {
                attachmentIDs += "'" + attachmentID + "',";
            }
            if (attachmentIDs.EndsWith(","))
            {
                attachmentIDs = attachmentIDs.Substring(0, attachmentIDs.Length - 1);
            }
            string sql = string.Format(@"select ATTACHMENTID,
       OBJECTTABLENAME,
       ATTACHMENTNAME,
       UPLOADTIME,
       ATTACHMENTSIZE,
       DOCUMENTTYPE,
       FILETYPEID,
       FILEURL,
       CREATEUSERID,
       REMARK
  from p_bt_attachments
  where OBJECTID in ({0})", attachmentIDs);
            DataSet oDataSet = DBHelper.ExecuteDataSet(sql);
            if (oDataSet != null && oDataSet.Tables.Count > 0)
            {
                return oDataSet.Tables[0];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取附件内容
        /// </summary>
        /// <param name="attachmentID"></param>
        /// <returns></returns>
        internal byte[] GetAttachmentContentByAttachmentID(string attachmentID)
        {
            byte[] rtnData = null;
            string sql = string.Format("select attachmentcontent from p_bt_attachments where attachmentid='{0}'", attachmentID);
            object result = this.DBHelper.ExecuteScalar(sql);
            if (result != null)
            {
                rtnData = (byte[])result;
            }
            return rtnData;
        }

        /// <summary>
        /// 获取附件上传用户对象
        /// </summary>
        /// <param name="attachmentID"></param>
        /// <returns></returns>
        internal string GetAttachmentCreateUserIDByAttachmentID(string attachmentID)
        {
            string rtnUserID = string.Empty;
            string getUserID = string.Format("select createuserid from p_bt_attachments where attachmentid='{0}'", attachmentID);
            object result = this.DBHelper.ExecuteScalar(getUserID);
            if (result != null)
            {
                rtnUserID = result.ToString();
            }
            return rtnUserID;
        }
    }
}